package com.dingwen.treasure.file.service.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Pair;
import com.dingwen.treasure.file.config.M3U8Properties;
import com.dingwen.treasure.file.service.FileService;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Objects;

/**
 * 　SystemFileServiceImpl: TODO 完善
 * 　@author dingwen
 * 　@date 2022/7/4
 */
@Service("systemFileServiceImpl")
public class SystemFileServiceImpl implements FileService {


    /**
     * m3u8 文件目录配置
     */
    @Resource
    private M3U8Properties m3U8Properties;

    /**
     * 文件上传
     *
     * @param multipartFile 支持多文件
     * @return {@link Pair}<{@link String}, {@link String}> 临时文件全路径 m3u8文件路径
     */
    @SneakyThrows(Throwable.class)
    public Pair<String,String> upload(MultipartFile multipartFile) {
        // 创建目录
        String rootPath = new File(System.getProperty("user.dir")).getAbsolutePath();
        String tempFilePath = rootPath.concat(m3U8Properties.getTempPath());
        String m3u8FilePath = rootPath.concat(m3U8Properties.getM3u8Path());
        if (!FileUtil.exist(tempFilePath)) {
            FileUtil.mkdir(tempFilePath);
        }
        if (!FileUtil.exist(m3u8FilePath)) {
            FileUtil.mkdir(m3u8FilePath);
        }
        String fullFilePath = tempFilePath.concat(Objects.requireNonNull(multipartFile.getOriginalFilename()));
        File file = new File(fullFilePath);
        multipartFile.transferTo(file);
        return new Pair(fullFilePath,m3u8FilePath);
    }

    /**
     * 文件下载
     *
     * @param fileId   文件id
     * @param request  要求
     * @param response 回答
     */
    @Override
    public void download(Long fileId, HttpServletRequest request, HttpServletResponse response) {
    }

    @Override
    public void preview(Long fileId, HttpServletResponse response) {
    }

    /**
     * 删除文件
     *
     * @param fileId 文件id
     */
    @Override
    public void remove(Long fileId) {
    }
}
